import 'package:azring/gen/assets.gen.dart';
import 'package:flutter/material.dart';
import 'package:getx_scaffold/getx_scaffold.dart';
import 'shipping_address_logic.dart';

/// 收货地址页面
class ShippingAddressView extends GetView<ShippingAddressLogic> {
  const ShippingAddressView({super.key});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.black,
      body: SafeArea(
        child: Column(
          children: [
            _buildAppBar(),
            Expanded(
              child: Obx(() {
                if (controller.state.isLoading.value) {
                  return const Center(
                    child: CircularProgressIndicator(
                      color: Color(0xFFF67F41),
                    ),
                  );
                }
                
                if (controller.state.addressList.isEmpty) {
                  return Center(
                    child: Text(
                      '暂无收货地址',
                      style: TextStyle(
                        fontSize: 14.sp,
                        color: Colors.grey[500],
                      ),
                    ),
                  );
                }
                return ListView.builder(
                  padding: EdgeInsets.all(20.w),
                  itemCount: controller.state.addressList.length,
                  itemBuilder: (context, index) {
                    return _buildAddressCard(controller.state.addressList[index]);
                  },
                );
              }),
            ),
            _buildAddButton(),
          ],
        ),
      ),
    );
  }

  /// 顶部导航栏
  Widget _buildAppBar() {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 20.w, vertical: 16.h),
      child: Row(
        children: [
          GestureDetector(
            onTap: () => Get.back(),
            child: Icon(
              Icons.arrow_back_ios,
              color: Colors.white,
              size: 20.sp,
            ),
          ),
          Expanded(
            child: Text(
              '收货地址',
              textAlign: TextAlign.center,
              style: TextStyle(
                fontSize: 16.sp,
                fontWeight: FontWeight.w500,
                color: Colors.white,
              ),
            ),
          ),
          SizedBox(width: 20.w),
        ],
      ),
    );
  }

  /// 地址卡片
  Widget _buildAddressCard(Map<String, dynamic> address) {
    return Dismissible(
      key: Key(address['id'].toString()),
      direction: DismissDirection.endToStart,
      confirmDismiss: (direction) async {
        return await Get.dialog<bool>(
          AlertDialog(
            backgroundColor: const Color(0xFF282725),
            title: Text(
              '确认删除',
              style: TextStyle(
                fontSize: 16.sp,
                color: Colors.white,
              ),
            ),
            content: Text(
              '确定要删除这个地址吗?',
              style: TextStyle(
                fontSize: 14.sp,
                color: Colors.grey[400],
              ),
            ),
            actions: [
              TextButton(
                onPressed: () => Get.back(result: false),
                child: Text(
                  '取消',
                  style: TextStyle(
                    fontSize: 14.sp,
                    color: Colors.grey[400],
                  ),
                ),
              ),
              TextButton(
                onPressed: () => Get.back(result: true),
                child: Text(
                  '删除',
                  style: TextStyle(
                    fontSize: 14.sp,
                    color: const Color(0xFFF67F41),
                  ),
                ),
              ),
            ],
          ),
        ) ?? false;
      },
      onDismissed: (direction) {
        controller.deleteAddress(address['id']);
      },
      background: Container(
        margin: EdgeInsets.only(bottom: 16.h),
        padding: EdgeInsets.symmetric(horizontal: 20.w),
        decoration: BoxDecoration(
          color: Colors.red,
          borderRadius: BorderRadius.circular(12.r),
        ),
        alignment: Alignment.centerRight,
        child: Icon(
          Icons.delete,
          color: Colors.white,
          size: 24.sp,
        ),
      ),
      child: Container(
        margin: EdgeInsets.only(bottom: 16.h),
        padding: EdgeInsets.all(15.w),
        decoration: BoxDecoration(
          color: const Color(0xFF282725),
          borderRadius: BorderRadius.circular(12.r),
        ),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            // 姓名、电话和默认标签
            Row(
              children: [
                Text(
                  address['name'],
                  style: TextStyle(
                    fontSize: 14.sp,
                    fontWeight: FontWeight.w500,
                    color: Colors.white,
                  ),
                ),
                SizedBox(width: 5.w),
                Text(
                  address['phone'],
                  style: TextStyle(
                    fontSize: 12.sp,
                    color: Colors.white,
                  ),
                ),
                SizedBox(width: 12.w),
                if (address['isDefault'] == true)
                  Container(
                    padding: EdgeInsets.symmetric(horizontal: 10.w, vertical: 2.h),
                    decoration: BoxDecoration(
                      color: const Color(0xFF353535),
                      borderRadius: BorderRadius.circular(24.r),
                    ),
                    child: Text(
                      '默认',
                      style: TextStyle(
                        fontSize: 12.sp,
                        color: const Color(0xffF67F41),
                      ),
                    ),
                  ),
                const Spacer(),
                GestureDetector(
                  onTap: () => controller.editAddress(address),
                  child: Assets.points.edit.image(width:20.w,height: 20.h ),
                ),
              ],
            ),
            SizedBox(height: 12.h),
            // 地址
            Text(
              address['address'],
              style: TextStyle(
                fontSize: 14.sp,
                color: const Color(0xffF7F7F7),
              ),
            ),
          ],
        ),
      ),
    );
  }

  /// 添加地址按钮
  Widget _buildAddButton() {
    return Container(
      padding: EdgeInsets.all(34.w),
      child: GestureDetector(
        onTap: controller.addNewAddress,
        child: Container(
          width: double.infinity,
          padding: EdgeInsets.symmetric(vertical: 16.h),
          decoration: BoxDecoration(
            color: const Color(0xFF353535),
            borderRadius: BorderRadius.circular(24.r),
          ),
          child: Row(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Icon(
                Icons.add,
                color: const Color(0xFFF67F41),
                size: 20.sp,
              ),
              SizedBox(width: 8.w),
              Text(
                '新增收货地址',
                style: TextStyle(
                  fontSize: 16.sp,
                  color: const Color(0xFFF67F41),
                  fontWeight: FontWeight.w500,
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
